import { createRouter, createWebHistory } from 'vue-router'

import { Vue_App_Token } from '@/utils/config'
import { getCookie } from '@/utils/Storage.js'
import { routes } from '@/router/index'
import store from '@/store'

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
})

router.beforeEach((to, from, next) => {

  console.log(getCookie(Vue_App_Token));

  if (getCookie(Vue_App_Token) != null || to.path === '/about') {
    store.commit('SetRegusterStatus', false)

  } else {
    store.commit('SetRegusterStatus', true)

  }
  console.log(store.state.RegusterStatus);



  if (to.path === '/manage') {
    store.commit('SetLayoutStatus', false)
  } else {
    store.commit('SetLayoutStatus', true)
  }

  // if (to.path === '/about') {
  //   store.commit('SetRegusterStatus', false)
  // } else {
  //   store.commit('SetRegusterStatus', true)
  // }
  if (to.path === '/reguser' || to.path === '/about' || to.path === '/') return next()
  if (to.path === '/login') {
    if (getCookie(Vue_App_Token)) {
      next('/')
    } else {

      next()
    }
  } else {
    if (!getCookie(Vue_App_Token)) {
      next('/login')
    } else {
      next()
    }
  }
})

//路由注册和导航守卫
export default router
